﻿#include "AppTool.h"

#import "AmapLocationTool.h"
#import "AmapSearchTool.h"

static AppTool* mAppTool = NULL;

static AmapLocationTool* amapLocTool = nil;
static AmapSearchTool* amapSearchTool = nil;

AppTool::AppTool() : m_pAmapLocationDelegate(NULL), m_pGeocodeDelegate(NULL), m_pPoiSearchDelegate(NULL)
{
    
}

AppTool::~AppTool()
{

}

AppTool* AppTool::getInstance()
{
	if (mAppTool == NULL)
	{
		mAppTool = new AppTool();
	}

	return mAppTool;
}

void AppTool::destroyInstance()
{
    if (amapLocTool != nil)
    {
        [amapLocTool stopLocation];
        [amapLocTool release];
        amapLocTool = nil;
    }
    
    if (amapSearchTool != nil)
    {
        [amapSearchTool release];
        amapSearchTool = nil;
    }
    
	CC_SAFE_RELEASE_NULL(mAppTool);
}

void AppTool::startOnceLocation()
{
	if (amapLocTool == nil)
    {
        amapLocTool = [[AmapLocationTool alloc] init];
    }
    
    [amapLocTool startOnceLocation];
}

void AppTool::startLocation(int interval)
{
    if (amapLocTool == nil)
    {
        amapLocTool = [[AmapLocationTool alloc] init];
    }
    
    [amapLocTool startLocation:interval];
}

void AppTool::stopLocation()
{
    [amapLocTool stopLocation];
}

void AppTool::getGeocode(const string& addr, const string& city)
{
	if (amapSearchTool == nil)
    {
        amapSearchTool = [[AmapSearchTool alloc] init];
    }
    
    [amapSearchTool getGeocodeWithAddress:[NSString stringWithUTF8String:addr.c_str()] City:[NSString stringWithUTF8String:city.c_str()]];
}

void AppTool::getReGeocode(double lat, double lng, float radius)
{
    if (amapSearchTool == nil)
    {
        amapSearchTool = [[AmapSearchTool alloc] init];
    }
    
    [amapSearchTool getRegeocodeWithLat:lat Lng:lng Radius:(int)radius];
}

void AppTool::poiKeySearch(const string& keystr, const string& poitype, const string& city)
{
    if (amapSearchTool == nil)
    {
        amapSearchTool = [[AmapSearchTool alloc] init];
    }
    
    [amapSearchTool poiSearchWithKeyWord:[NSString stringWithUTF8String:keystr.c_str()] POIType:[NSString stringWithUTF8String:poitype.c_str()] City:[NSString stringWithUTF8String:city.c_str()]];
}

void AppTool::poiAroundSearch(const string& keystr, const string& poitype, double lat, double lng, int round, string city)
{
    if (amapSearchTool == nil)
    {
        amapSearchTool = [[AmapSearchTool alloc] init];
    }
    
    [amapSearchTool poiSearchWithKeyWord:[NSString stringWithUTF8String:keystr.c_str()] POIType:[NSString stringWithUTF8String:poitype.c_str()] AroundLat:lat AroundLng:lng AroundRound:round City:[NSString stringWithUTF8String:city.c_str()]];
}
